package com.platform.server.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.platform.server.dao.VideoDao;
import com.platform.server.entity.TopicComment;
import com.platform.server.entity.User;
import com.platform.server.entity.VideoComment;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.service.VideoService;
import com.platform.server.util.StringTools;

@Service
@Transactional
public class VideoServiceImpl implements VideoService {

	@Autowired
	private VideoDao videoDao;
	
	/**
	 * 查看我的视频列表
	 */
	@Override
	public ReturnData selectVideoList(String id) {
		if(StringTools.isEmpty(id)){
			return new ReturnData(12,"参数有误");
		}
		List<Map<String, Object>> lists = videoDao.selectVideoList(id);
		if(lists.size() > 0){
			return new ReturnData(lists,200,"成功");
		}
		return new ReturnData(100,"暂无数据");
	}

	/**
	 * 个人视频详情
	 */
	@Override
	public ReturnData selectCommentList(String videoid) {
		if(StringTools.isEmpty(videoid)){
			return new ReturnData(12,"参数有误");
		}
		List<VideoComment> lists = videoDao.selectCommentList(videoid);
		if(lists.size() == 0){
			return new ReturnData(100,"该视频暂无评论");
		}
		for(int i = 0;i < lists.size();i++){
			//查看话题评论中的父评论
			VideoComment videoComment = videoDao.queryParentList(Integer.parseInt(lists.get(i).getParent_id()));
			lists.get(i).setVideoComment(videoComment);
			//查看评论人信息
			User suser = videoDao.selectSenderInfo(Integer.parseInt(lists.get(i).getSender_id()));
			lists.get(i).setSenderUser(suser);
			//查看回复人信息
			String rid = lists.get(i).getReceiver_id();
			if(!StringTools.isEmpty(rid)){
				User ruser = videoDao.selectSenderInfo(Integer.parseInt(rid));
				lists.get(i).setReceiverUser(ruser);
			}
		}
		return new ReturnData(lists,200,"成功");
	}

	/**
	 * 删除视频
	 */
	@Override
	public ReturnData deleteVideo(Integer videoid) {
		if(videoid == null){
			return new ReturnData(12,"参数有误");
		}
		int flag = videoDao.deleteVideo(videoid);
		if(flag > 0){
			return new ReturnData(200,"成功");
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 视频点赞
	 */
	@Override
	public ReturnData videoLike(String userid, String videoid) {
		if(StringTools.isEmpty(videoid) || StringTools.isEmpty(userid)){
			return new ReturnData(12,"参数有误");
		}
		//查看用户是否已经点赞
		int count = videoDao.selectIsLike(userid, videoid);
		if(count > 0){
			return new ReturnData(20,"已赞过");
		}
		int flag = videoDao.videoLike(userid, videoid);
		if(flag > 0){
			return new ReturnData(200,"成功");
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 发表视频评论
	 */
	@Override
	public ReturnData insertVideoComment(VideoComment videoComment) {
		if(StringTools.isEmpty(videoComment.getVideo_id()) || StringTools.isEmpty(videoComment.getSender_id())
				|| StringTools.isEmpty(videoComment.getContent())){
			return new ReturnData(12,"参数有误");
		}
		videoComment.setCreate_time(new Date());
		VideoComment topicComment2 = null;
		if(!StringTools.isEmpty(videoComment.getParent_id())){
			//先找到父评论
			topicComment2 = videoDao.selectParentComment(Integer.parseInt(videoComment.getParent_id())); 
            if(topicComment2 == null){
                return new ReturnData(10,"未找到你要评论的评论");
            }else {
           	 //设置该评论路径
                if(StringTools.isEmpty(topicComment2.getPath())){  //设置评论路径。如果父评论是一级评论
                	videoComment.setPath(","+topicComment2.getId()+",");
                }else { //如果父评论是多级评论,就在父评论的基础上再加一级
                	videoComment.setPath(topicComment2.getPath()+topicComment2.getId()+",");
                }
            }
        }
		int flag = videoDao.insertVideoComment(videoComment);
		if(flag == 1){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("recordid", videoComment.getId());
			//评论或回复成功之后添加消息表记录
			int i = videoDao.insertCommentRecord(map);
			if(i == 1){
				return new ReturnData(200,"成功");
			}
		}
		return new ReturnData(100,"失败");
	}

	/**
	 * 删除视频评论
	 */
	@Override
	public ReturnData deleteVideoComment(Integer id) {
		if(id == null){
			return new ReturnData(12,"参数有误");
		}
		int flag = videoDao.deleteVideoComment(id);
		if(flag > 0){
			return new ReturnData(200,"删除成功");
		}
		return new ReturnData(100,"删除失败");
	}

}
